<?php

namespace App\Console\Commands\OpenShop\Ekb;

use App\Remote\EKuaiBao\EKuaiBaoRemote;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;

class FixBankCardToPublicCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ekb:fixBankCardToPublicCommand {bankCardNos}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '易快报 - 修改银行卡权限为全部可见';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     * @throws GuzzleException
     */
    public function handle(): int
    {
        $bankCardNosStr = $this->input->getArgument("bankCardNos");
        $bankCardNosArr = explode(",", $bankCardNosStr);
        collect($bankCardNosArr)->each(function ($bankCardNo) {
            try {
                $payeeInfos  = EKuaiBaoRemote::payeeInfos($bankCardNo, true);
                $payeeInfo   = current($payeeInfos["items"]);
                $fullVisible = attributeGet($payeeInfo, "visibility.fullVisible", null);
                if (is_bool($fullVisible) && $fullVisible === false) {
                    EKuaiBaoRemote::updatePayeeInfos(
                        $payeeInfo["id"],
                        $payeeInfo["cardNo"],
                        $payeeInfo["name"],
                        $payeeInfo["branch"],
                        $payeeInfo["operatorId"],
                        true
                    );
                }
            } catch (\Exception $exception) {
                $this->output->info("[ {$bankCardNo} ]  " . $exception->getMessage());
            }

        });
        return 0;
    }
}
